﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OBDUDSUtils.CNNMode;
using System.IO;

namespace OBDUDSUtils.Common
{
    class UpdatePlatform
    {
        public static List<PlatformMessage> ReadPlatformMessage(string file)
        {
            if (!File.Exists(file))
                return new List<PlatformMessage>();
            List<PlatformMessage> data = new List<PlatformMessage>();
            using (FileStream fs = new FileStream(file, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        var msg = sr.ReadLine().Trim();
                        if (string.IsNullOrEmpty(msg))
                            continue;
                        var msgs = msg.Split(',');
                        PlatformMessage message = new PlatformMessage();
                        message.Platforms = msgs[0];
                        message.ScriptName = msgs[1];
                        message.SelectSafeVerify = msgs[2];
                        message.UpdateDate = msgs[3];
                        data.Add(message);
                    }
                }
            }
            return data;
        }

        public static List<string> ReadScriptMessage(string file)
        {
            if (!File.Exists(file))
                return new List<string>();
            List<string> data = new List<string>();
            using (FileStream fs = new FileStream(file, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        var msg = sr.ReadLine().Trim();
                        if (string.IsNullOrEmpty(msg))
                            continue;
                        data.Add(msg);
                    }
                }
            }
            return data;
        }

        public static int WritePlatformMessage(string file, List<PlatformMessage> data)
        {
            int iCount = 0;
            using (FileStream fs = new FileStream(file, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (var msg in data)
                    {
                        sw.WriteLine($"{msg.Platforms},{msg.ScriptName},{msg.SelectSafeVerify},{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                        iCount++;
                    }
                }
            }
            return iCount;
        }
    }
}
